package com.lwl.leetcode;

import org.junit.Test;

/**
 * @author lwl
 * @Description TODO
 * @date 2023/8/2 8:37
 */
public class Code45Jump {
    public int jump(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int step = 1;
        int maxRight = nums[0];
        int nextMaxRight = 0;
        for (int i = 1; i < nums.length - 1; i++) {
            nextMaxRight = Math.max(nextMaxRight, i + nums[i]);
            if (i == maxRight) {
                step++;
                maxRight = nextMaxRight;
            }
        }
        return step;
    }

    @Test
    public void test() {
        System.out.println(jump(new int[]{2, 3, 1, 1, 4}));
        System.out.println(jump(new int[]{2, 3, 0, 1, 4, 1, 1}));
        System.out.println(jump(new int[]{2, 3, 0, 1, 1, 1, 1}));
    }
}
